package dao;

// Generated 2 juin 2012 15:20:31 by Hibernate Tools 3.4.0.CR1

import static org.hibernate.criterion.Example.create;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import beans.Client;
import beans.Passager;
import dao.interfaces.IDaoClient;



/**
 * Home object for domain model class Client.
 * @see beans.Client
 * @author Hibernate Tools
 */
public class DaoClient implements IDaoClient {

	private static final Log log = LogFactory.getLog(DaoClient.class);

	private final SessionFactory sessionFactory = getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return HibernateUtil.getSessionFactory();
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoClient#persist(beans.Client)
	 */
	@Override
	public void persist(Client transientInstance) {
		log.debug("persisting Client instance");
		try {

			sessionFactory.getCurrentSession().persist(transientInstance);
			
			
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(Client instance) {
		log.debug("attaching dirty Client instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Client instance) {
		log.debug("attaching clean Client instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoClient#delete(beans.Client)
	 */
	@Override
	public void delete(Client persistentInstance) {
		log.debug("deleting Client instance");
		try {
			
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoClient#merge(beans.Client)
	 */
	@Override
	public Client merge(Client detachedInstance) {
		log.debug("merging Client instance");
		try {
			Client result = (Client) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see dao.IDaoClient#findById(int)
	 */
	@Override
	public Client findById(int id) {
		log.debug("getting Client instance with id: " + id);
		try {
			Client instance = (Client) sessionFactory.getCurrentSession().get(
					"beans.Client", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;

		}
	}

	
	@Override
	public Client getClient(String login,String password) {
		
		Query q = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("from Client"); 
		List<Client> list = q.list();
		for(Client c: list){

			if( (c.getCliMail().equals(login)) &&(c.getCliMotPasse().equals(password))){
				return c;
			}
		}
		return null;
	}
}
